package br.com.oxenti.osiga.processo.ui.presenter;

import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import br.gov.frameworkdemoiselle.event.ProcessClear;
import br.gov.frameworkdemoiselle.event.ProcessDelete;
import br.gov.frameworkdemoiselle.event.ProcessItemSelection;
import br.gov.frameworkdemoiselle.event.BeforeNavigateToView;
import br.gov.frameworkdemoiselle.event.ProcessSave;
import br.gov.frameworkdemoiselle.template.AbstractPresenter;
import br.gov.frameworkdemoiselle.stereotype.ViewController;

import br.com.oxenti.osiga.processo.business.ProcessoBC;
import br.com.oxenti.osiga.processo.domain.Processo;
import br.com.oxenti.osiga.processo.ui.view.ProcessoView;

@ViewController
@SessionScoped
public class ProcessoPresenter extends AbstractPresenter<ProcessoView> {

	private static final long serialVersionUID = 1L;

	@Inject
	private ProcessoBC processoBC;

	public void onSave(@Observes @ProcessSave Processo processo) {
		if (processo.getId() != null) {
			processoBC.update(processo);
		} else {
			processoBC.insert(processo);
		}
		getView().clear();
		getView().setDeleteButtonEnabled(false);
		getView().setList(processoBC.findAll());
	}

	public void onItemSelection(@Observes @ProcessItemSelection Processo processo) {
		getView().setBean(processo);
		getView().setDeleteButtonEnabled(true);
	}

	public void onDelete(@Observes @ProcessDelete Processo processo) {
		processoBC.delete(processo.getId());
		getView().setDeleteButtonEnabled(false);
		getView().setList(processoBC.findAll());
	}

	public void onSearch(@Observes @BeforeNavigateToView ProcessoView view) {
		view.setList(processoBC.findAll());
	}

	public void onFormClear(@Observes @ProcessClear Processo processo) {
		getView().clear();
		getView().setDeleteButtonEnabled(false);
	}
	
}
